﻿using System;

namespace PortableTPL
{
    public partial class Task<T>
    {
        public Task<T> ContinueWith(Func<Task<T>, T> continueAction, TaskContinuationOptions taskContinuationOptions = TaskContinuationOptions.None, TaskScheduler scheduler = null)
        {
            ContinueTask = new Task<T>(() =>
            {
                return continueAction(this);
            });
            PrepareContinuation(scheduler, taskContinuationOptions);

            return (Task<T>)ContinueTask;
        }

        public Task ContinueWith(Action<Task<T>> continueAction, TaskContinuationOptions taskContinuationOptions = TaskContinuationOptions.None, TaskScheduler scheduler = null)
        {
            ContinueTask = new Task(() => continueAction(this));

            PrepareContinuation(scheduler, taskContinuationOptions);

            return ContinueTask;
        }

        public Task<T> ContinueWith(Func<Task<T>, T> continueAction, CancellationToken token, TaskContinuationOptions taskContinuationOptions = TaskContinuationOptions.None, TaskScheduler scheduler = null)
        {
            ContinueTask = new Task<T>(() =>
            {
                return continueAction(this);
            }, token);
            PrepareContinuation(scheduler, taskContinuationOptions);

            return (Task<T>)ContinueTask;
        }

        public Task ContinueWith(Action<Task<T>> continueAction, CancellationToken token, TaskContinuationOptions taskContinuationOptions = TaskContinuationOptions.None, TaskScheduler scheduler = null)
        {
            ContinueTask = new Task(() => continueAction(this), token);

            PrepareContinuation(scheduler, taskContinuationOptions);

            return ContinueTask;
        }
    }
}
